import { dayjs } from "element-plus";

/**
 * 修改后端返回的日期格式
 * @param date 日期
 * @returns 返回xxxx年xx月xx日xx时xx分xx秒
 */
export const formatDateTime = (time) => {
  if (!time) return
  const date = new Date(time)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  return `${year}年${month}月${day}日${hours}时${minutes}分${seconds}秒`
}

/**
 * 修改后端返回的日期格式
 * @param date 日期
 * @returns 返回xxxx年xx月xx日
 */
export const formatDate = (time) => {
  if (!time) return
  const date = new Date(time)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${year}年${month}月${day}日`
}

/**
 * 计算2个日期时间的差异（也可以用来比较2个时间的大小）
 *
 * @param {string} d1 日期1
 * @param {string} d2 日期2
 * @returns 返回差异信息
 */
export const diff = (d1, d2, unit) => {
  d1 = d1.replace(/\//g, "-");
  d2 = d2.replace(/\//g, "-");
  const dt1 = dayjs(d1);
  const dt2 = dayjs(d2);
  return dt1.diff(dt2, unit);
};

/**
 * 根据日期获取星期几
 *
 * @param dateStr 日期
 */
export const getWeekDay = (dateStr) => {
  const weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  const date = new Date(dateStr);
  const weekDays = date.getDay();
  return weeks[weekDays];
};

/**
 * 获取现在日期
 *
 */
export const getCurrentDate = (dateStr) => {
  const now = new Date();
  const year = now.getFullYear();
  const month = (now.getMonth() + 1).toString().padStart(2, '0');
  const day = now.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`;
};